'use strict';

angular.module('CBO').controller('MOController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', 'ExtendEnumService', 'BaseConst', '$location', 'Authentication',
    function($scope, $stateParams, BaseModule, BaseFunction, ExtendEnumService, BaseConst, $location, Authentication) {
        $scope.MOStatusEnum = BaseFunction.getEnum('ProduceReadyEnum.MOStatus');
        ExtendEnumService.getExtendEnum('ItemCategory', function(extendEnum){
            $scope.ItemCategoryEnum = extendEnum;
        });

        $scope.btnSave = function(){
            if($scope.Module.MO.FocusedRecord.Code === undefined || 
                $scope.Module.MO.FocusedRecord.Code.trim() === ""){
                BaseFunction.alert("请录入编码", "保存提示");
                return;
            }
            if($scope.Module.MO.FocusedRecord.ItemMaster === undefined || 
                $scope.Module.MO.FocusedRecord.ItemMaster === null){
                BaseFunction.alert("请录入物料", "保存提示");
                return;
            }
            if($scope.Module.MO.FocusedRecord.Qty === undefined || 
                $scope.Module.MO.FocusedRecord.Qty === null || 
                $scope.Module.MO.FocusedRecord.Qty <= 0){
                BaseFunction.alert("生产数量不能为0", "保存提示");
                return;
            }
            if($scope.Module.MO.FocusedRecord.StartDateTime === undefined || 
                $scope.Module.MO.FocusedRecord.StartDateTime === null || 
                $scope.Module.MO.FocusedRecord.StartDateTime == BaseConst.DateTime.Min){
                BaseFunction.alert("请录入计划开工日期", "保存提示");
                return;
            }
            if($scope.Module.MO.FocusedRecord.EndDateTime === undefined || 
                $scope.Module.MO.FocusedRecord.EndDateTime === null || 
                $scope.Module.MO.FocusedRecord.EndDateTime == BaseConst.DateTime.Min){
                BaseFunction.alert("请录入计划完工日期", "保存提示");
                return;
            }
            if(new Date($scope.Module.MO.FocusedRecord.StartDateTime) > new Date($scope.Module.MO.FocusedRecord.EndDateTime)){
                BaseFunction.alert("计划开工日期不能大于计划完工日期", "保存提示");
                return;
            }

            $scope.Module.MO.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.MO.cancel();            
        };
        $scope.btnAdd = function(){
            $scope.Module.MO.add();
        };
        $scope.btnDelete = function(){
            $scope.Module.MO.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/MO/List');
        };

        //初始化
        BaseModule.initModule($scope, 'MO');
        $scope.Module.MO.Populate = {path: 'ItemMaster', select: 'Code Name'};
        $scope.Module.MO.AfterOnAdd = function(record){
            record.Status = $scope.MOStatusEnum.Start.Value;
            record.StartDateTime = new Date();
            record.OnlineQty = 0;
            record.TotalCompleteQty = 0;
        };

        if($stateParams.id) {
            $scope.Module.MO.findById($stateParams.id, function(record){
                $scope.Module.MO.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        }
    }
]);
